Conversation
📝 WalkthroughWalkthrough세 개의 엔티티 클래스(GroupBuyPost, User)와 서비스 클래스(ReviewApplicationService)에서 rating 관련 필드들을 primitive 타입에서 wrapper 타입으로 변경하는 작업입니다. User 엔티티의 경우 데이터베이스 컬럼의 nullable 속성도 false에서 true로 조정되었습니다. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
기술적 검토전체적으로 primitive type에서 wrapper type으로의 전환은 일관성 있게 적용되었는데, 몇 가지 짚고 넘어갈 부분이 있네요: 1. Nullability 처리 재검토 2. GroupBuyPost의 초기값 3. EMA 도입과의 연계 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@src/main/java/org/sopt/poti/domain/groupbuy/entity/GroupBuyPost.java`:
- Around line 75-82: The rating fields ratingAvg, ratingSum, and ratingCount can
be null (DB nullable) which risks NPEs in addRating due to auto-unboxing; update
addRating to defensively handle nulls by treating null as 0 (e.g., coerce
ratingSum/ratingCount/ratingAvg to 0 before arithmetic) or add a
`@PostLoad/`@PrePersist initializer that backfills null fields to 0, and ensure
any setters also guard against null inputs; reference the fields ratingAvg,
ratingSum, ratingCount and the addRating method when applying the fix.
In
`@src/main/java/org/sopt/poti/domain/review/application/ReviewApplicationService.java`:
- Around line 44-45: The code in ReviewApplicationService reads Integer
reviewCount = post.getRatingCount() and Double postAvg = post.getRatingAvg() but
GroupBuyPost's rating fields can be null; update ReviewApplicationService to
null-safe defaults (e.g., reviewCount -> 0, postAvg -> 0.0) before any
arithmetic/logic, and use those non-null primitives or safe wrappers when
calling downstream methods; apply the same null-coalescing fix to the other
occurrence referenced (the second block around lines 50) so both
reviewCount/postAvg usages are protected.
In `@src/main/java/org/sopt/poti/domain/user/entity/User.java`:
- Around line 59-63: The User entity's ratingSum/ratingCount are declared
nullable which risks NPEs where callers (ReviewApplicationService: reviewCount =
post.getRatingCount(); and GroupBuyPost methods updating/reading ratings)
perform unboxing or arithmetic; fix by making the DB columns non-nullable and
preserving the zero defaults: set `@Column`(name = "rating_sum", nullable = false)
and `@Column`(name = "rating_count", nullable = false) on User.ratingSum and
User.ratingCount (keep the initializers 0L/0), and then audit
usages—specifically ReviewApplicationService where reviewCount is assigned and
GroupBuyPost methods that do this.ratingSum += score / this.ratingCount += 1 /
divisions—to rely on non-null values (no further null checks required after this
change) or, if you prefer code-side hardening instead, wrap reads with
Objects.requireNonNullElse(post.getRatingCount(), 0) and
Objects.requireNonNullElse(post.getRatingSum(), 0L) in ReviewApplicationService
and use the same null-safe reads inside GroupBuyPost before mutating or
dividing.
| @Column(name = "rating_avg", nullable = true) | ||
| private double ratingAvg = 0.0; //공구 글 당 평점 | ||
| private Double ratingAvg = 0.0; //공구 글 당 평점 | ||
|
|
||
| @Column(name = "rating_sum", nullable = true) | ||
| private long ratingSum = 0L; | ||
| private Long ratingSum = 0L; | ||
|
|
||
| @Column(name = "rating_count", nullable = true) | ||
| private int ratingCount = 0; | ||
| private Integer ratingCount = 0; |
There was a problem hiding this comment.
Nullable로 바뀐 rating 필드가 null이면 addRating에서 NPE 가능성 있습니다.
컬럼이 nullable이면 기존 데이터가 null로 로드될 수 있고, += 연산에서 오토 언박싱으로 NPE가 발생할 수 있습니다. 최소한 addRating 진입 시 null-safe 보정(또는 @PostLoad/@PrePersist, 백필) 처리가 필요합니다.
🛠️ 제안 수정
public void addRating(int score) {
validateScore(score);
+ if (this.ratingSum == null) this.ratingSum = 0L;
+ if (this.ratingCount == null) this.ratingCount = 0;
+ if (this.ratingAvg == null) this.ratingAvg = 0.0;
+
this.ratingSum += score;
this.ratingCount += 1;
- Double avg = (double) this.ratingSum / this.ratingCount;
+ double avg = (double) this.ratingSum / this.ratingCount;
this.ratingAvg = Math.round(avg * 10) / 10.0;
}Also applies to: 210-218
🤖 Prompt for AI Agents
In `@src/main/java/org/sopt/poti/domain/groupbuy/entity/GroupBuyPost.java` around
lines 75 - 82, The rating fields ratingAvg, ratingSum, and ratingCount can be
null (DB nullable) which risks NPEs in addRating due to auto-unboxing; update
addRating to defensively handle nulls by treating null as 0 (e.g., coerce
ratingSum/ratingCount/ratingAvg to 0 before arithmetic) or add a
`@PostLoad/`@PrePersist initializer that backfills null fields to 0, and ensure
any setters also guard against null inputs; reference the fields ratingAvg,
ratingSum, ratingCount and the addRating method when applying the fix.
| Integer reviewCount = post.getRatingCount(); | ||
| Double postAvg = post.getRatingAvg(); |
There was a problem hiding this comment.
postAvg/reviewCount null 가능성 처리 필요합니다.
GroupBuyPost의 rating 필드가 nullable로 바뀌어 null이 반환될 수 있습니다. 그대로 전달하면 내부 연산에서 NPE/오동작 위험이 있습니다. null-safe 기본값 보정 후 전달하는 편이 안전합니다.
🛠️ 제안 수정
GroupBuyPost post = groupBuyService.getPostWithLock(postId);
Integer reviewCount = post.getRatingCount();
Double postAvg = post.getRatingAvg();
+Integer safeReviewCount = (reviewCount != null) ? reviewCount : 0;
+Double safePostAvg = (postAvg != null) ? postAvg : 0.0;
int postCount = groupBuyService.countPostsByLeader(sellerId);
// 3 판매자 평점 반영
-userService.applyPostContribution(sellerId, postId, postAvg, reviewCount, postCount);
+userService.applyPostContribution(sellerId, postId, safePostAvg, safeReviewCount, postCount);Also applies to: 50-50
🤖 Prompt for AI Agents
In
`@src/main/java/org/sopt/poti/domain/review/application/ReviewApplicationService.java`
around lines 44 - 45, The code in ReviewApplicationService reads Integer
reviewCount = post.getRatingCount() and Double postAvg = post.getRatingAvg() but
GroupBuyPost's rating fields can be null; update ReviewApplicationService to
null-safe defaults (e.g., reviewCount -> 0, postAvg -> 0.0) before any
arithmetic/logic, and use those non-null primitives or safe wrappers when
calling downstream methods; apply the same null-coalescing fix to the other
occurrence referenced (the second block around lines 50) so both
reviewCount/postAvg usages are protected.
| @Column(name = "rating_sum", nullable = true) | ||
| private Long ratingSum = 0L; | ||
|
|
||
| @Column(name = "rating_count", nullable = false) | ||
| private int ratingCount = 0; | ||
| @Column(name = "rating_count", nullable = true) | ||
| private Integer ratingCount = 0; |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Find all usages of ratingSum and ratingCount in the codebase
rg -n --type java -C3 '(getRatingSum|getRatingCount|ratingSum|ratingCount)' --max-count=100Repository: team-poti/POTI-SERVER
Length of output: 3802
🏁 Script executed:
#!/bin/bash
# Check if Lombok is used and look for getter method patterns
rg -n --type java '@Getter|@Data|getRatingSum|getRatingCount' src/main/java/org/sopt/poti/domain/user/entity/User.java src/main/java/org/sopt/poti/domain/groupbuy/entity/GroupBuyPost.java -A2 -B2
echo "---"
# Search for all method calls and usages beyond the initial results
rg -n --type java 'getRatingSum|getRatingCount' --max-count=200Repository: team-poti/POTI-SERVER
Length of output: 1083
🏁 Script executed:
#!/bin/bash
# Check ReviewApplicationService context around the usage
cat -n src/main/java/org/sopt/poti/domain/review/application/ReviewApplicationService.java | sed -n '35,65p'
echo "---"
# Also check for any arithmetic operations or method calls on ratingCount/ratingSum
rg -n --type java '(ratingCount|ratingSum)\s*[+\-*/=<>]|ratingCount\s*\)|\+\s*(ratingCount|ratingSum)' -A2 -B2Repository: team-poti/POTI-SERVER
Length of output: 3059
ratingSum/ratingCount nullable 설정 시 NPE 위험성 확인 필요
DB 컬럼이 nullable = true로 설정되어 있는데, 코드에서 null 체크 없이 언박싱 연산을 수행하고 있습니다.
- ReviewApplicationService.java L44:
Integer reviewCount = post.getRatingCount();- null이 반환되면 int 할당 시 NPE 발생 - GroupBuyPost.java L213-217:
this.ratingSum += score;,this.ratingCount += 1;, 나눗셈 연산 등에서 null 값 처리 없음
마이그레이션이나 기존 데이터에 null이 존재한다면 런타임 오류가 발생할 수 있으니, 다음 중 하나를 확인/적용해주세요:
- 모든 사용처에 null 안전성 처리 추가
- DB 마이그레이션으로 기존 null 값을 기본값으로 변경
nullable = false로 변경 (기본값 설정이 이미 되어 있으므로)
🤖 Prompt for AI Agents
In `@src/main/java/org/sopt/poti/domain/user/entity/User.java` around lines 59 -
63, The User entity's ratingSum/ratingCount are declared nullable which risks
NPEs where callers (ReviewApplicationService: reviewCount =
post.getRatingCount(); and GroupBuyPost methods updating/reading ratings)
perform unboxing or arithmetic; fix by making the DB columns non-nullable and
preserving the zero defaults: set `@Column`(name = "rating_sum", nullable = false)
and `@Column`(name = "rating_count", nullable = false) on User.ratingSum and
User.ratingCount (keep the initializers 0L/0), and then audit
usages—specifically ReviewApplicationService where reviewCount is assigned and
GroupBuyPost methods that do this.ratingSum += score / this.ratingCount += 1 /
divisions—to rely on non-null values (no further null checks required after this
change) or, if you prefer code-side hardening instead, wrap reads with
Objects.requireNonNullElse(post.getRatingCount(), 0) and
Objects.requireNonNullElse(post.getRatingSum(), 0L) in ReviewApplicationService
and use the same null-safe reads inside GroupBuyPost before mutating or
dividing.
📌 관련 이슈
✨ 변경 사항
📸 테스트 증명 (필수)
📚 리뷰어 참고 사항
✅ 체크리스트
Summary by CodeRabbit
릴리스 노트
✏️ Tip: You can customize this high-level summary in your review settings.